package com.tfjybj.itoo.teach.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.teach.entity.RoomEntity;
import com.tfjybj.itoo.teach.model.RoomModel;
import com.tfjybj.itoo.teach.provider.service.RoomService;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * RoomController
 * room表
 *
 * @author 田成荣
 * @version 2.0.0
 * @since 2.0.0 2018-11-08 19:34:10
 */
@Api(tags = {"room表接口"})
@RequestMapping(value = "/room")
@RestController
public class RoomController {

    @Resource
    private RoomService roomService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model RoomModel
     * @return 添加的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody RoomModel model) {
		if (StringUtils.isEmpty(model.getCode())){
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
		if (StringUtils.isEmpty(model.getName())){
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        // 判断添加的建筑Code和建筑名称是否重复
        String isRoomExist=roomService.findIdIsExist(model.getName(),model.getCode());
        if(!StringUtils.isEmpty(isRoomExist)){
            return ItooResult.build(ItooResult.FAIL,"添加失败，房间名称或者房间Code重复！");
        }else {
            RoomEntity roomEntity = new RoomEntity();
            BeanUtils.copyProperties(model, roomEntity);
            roomService.save(roomEntity);
            return ItooResult.build(ItooResult.SUCCESS, "添加成功");
        }

    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功          
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        roomService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        roomService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model RoomModel
     * @return 修改后的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id修改room")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody RoomModel model) {
		if (StringUtils.isEmpty(model.getCode())){
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
		if (StringUtils.isEmpty(model.getName())){
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
            RoomEntity roomEntity = new RoomEntity();
            BeanUtils.copyProperties(model, roomEntity);
            roomService.updateById(roomEntity);
            return ItooResult.build(ItooResult.SUCCESS, "修改成功");

    }

    /**
     * 根据id查找Room
     *
     * @param id 主键id
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        RoomEntity roomEntity = roomService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", roomEntity);
    }

    /**
     * 分页查询所有Room
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "分页查询所有Room")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo",value = "页码",required = true,example = "1")@PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize",value = "页数",required = true,example = "10")@PathVariable Integer pageSize) {
        PageInfo<RoomEntity> rooms = roomService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", rooms);
    }
	
	/**
     * 根据房间代码模糊查询room
     *
     * @param code  房间代码
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的room
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据房间代码模糊查询room", notes = "分页根据房间代码模糊查询room")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "int", required = true, example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "记录数", dataType = "int", required = true, example = "10")
    })
    @GetMapping(value = "queryRoomByLikeCode/{pageNo}/{pageSize}")
    public ItooResult queryRoomByLikeCode(@RequestParam(required = false, defaultValue = "") String code,
                                          @PathVariable Integer pageNo, @PathVariable Integer pageSize) {
        PageInfo<RoomEntity> roomList = roomService.queryByLikeCode(code, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", roomList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */
    /**
     * 根据建筑物ID查询所有可用的房间
     * @param buildingId  建筑Id
     * @param pageNo   页码
     * @param pageSize 页数
     * @return buildingModel
     * @author 段恩刚
     * @since 2.0.0 2018-11-16 19:34:10
     */
    @ApiOperation(value = "根据建筑物ID查询所有可用的房间", notes = "根据建筑物ID查询所有可用的房间")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "int", required = true, example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "记录数", dataType = "int", required = true, example = "10"),
            @ApiImplicitParam(name = "buildingId",value="建筑物id",dataType = "string",required = true,example = "1069881859854356481")
    })
    @GetMapping(value="queryRoomByBuildingId/{buildingId}/{pageNo}/{pageSize}")
    public ItooResult queryRoomByBuildingId(@PathVariable String buildingId, @PathVariable Integer pageNo, @PathVariable Integer pageSize){
        if (!buildingId.isEmpty()){
            PageInfo<RoomModel> roomModels= roomService.queryRoomByBuildingId(buildingId,pageNo,pageSize);
            return ItooResult.build(ItooResult.SUCCESS, "查询成功",roomModels);
        }else{
            return ItooResult.build(ItooResult.FAIL,"查询失败");
        }
    }

    /**
     * 根据最新十条房间信息
     * @param
     * @return buildingModel
     * @author 段恩刚
     * @since 2.0.0 2018-11-18 19:34:10
     */
    @ApiOperation(value = "查询最新十条房间信息", notes = "查询最新十条房间信息")
    @GetMapping(value="queryNew10Rooms")
    public ItooResult queryNew10Rooms(){
        List<RoomModel> roomModels= roomService.queryNew10Rooms();
        return ItooResult.build(ItooResult.SUCCESS, "查询成功",roomModels);
    }

    /**
     * 根据Room名称Room类型模糊查询Room信息
     * @param roomName  房间名称
     * @param roomType  房间类型
     * @return roomModel
     * @author 段恩刚
     * @since 2.0.0 2018-11- 20 19:34:10
     */
    @ApiOperation(value = "根据Room名称Room类型模糊查询Room信息", notes = "根据Room名称Room类型模糊查询Room信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "int", required = true, example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "记录数", dataType = "int", required = true, example = "10"),
            @ApiImplicitParam(name="roomName",value="房间名称",dataType = "string",required = true,example = "阁"),
            @ApiImplicitParam(name="roomType",value="房间类型",dataType = "string",required = true,example = "1")
    })
    @GetMapping(value = "queryRoomsLikeNameOrType/{roomName}/{roomType}/{pageNo}/{pageSize}")
    public ItooResult queryRoomsLikeNameOrType(@RequestParam(required = false, defaultValue = "") String roomName,
                                               @RequestParam(required = false, defaultValue = "") String roomType,
                                               @PathVariable Integer pageNo, @PathVariable Integer pageSize) {
        PageInfo<RoomModel> roomModels = roomService.queryRoomsLikeNameOrType(roomName,roomType, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功",roomModels);
    }

    /**
     * 房间管理页面的导出
     * @param
     * @return roomModel
     * @author 段恩刚
     * @since 2.0.0 2018-11-25 19:34:10
     */
    @ApiOperation(value = "根据roomName导出room信息")
    @GetMapping(value = {"/exportRoomInfos"})
    public ItooResult exportRoomInfos(HttpServletResponse response,
                                      @RequestParam(required =false,defaultValue = "")String roomName){
        if (roomService.exportRoomInfos(roomName,response)){
            return ItooResult.build(ItooResult.SUCCESS,"导出成功");
        }else {
            return ItooResult.build(ItooResult.FAIL,"导出失败");
        }
    }


    /**
     * 根据Room类型查询Room信息
     * @param roomType  房间类型
     * @return <roomModel>list</roomModel>
     * @author 段恩刚
     * @since 2.0.0 2018-11- 27 19:34:10
     */
    @ApiOperation(value = "根据Room类型查询Room信息", notes = "根据Room类型查询Room信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="roomType",value="房间类型",dataType = "string",required = true,example = "1")
    })
    @GetMapping(value = "queryRoomsbyType/{roomType}")
    public ItooResult queryRoomsbyType(@RequestParam(required = false, defaultValue = "") String roomType) {
        List<RoomModel> roomModels = roomService.queryRoomsbyType(roomType);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功",roomModels);
    }

    /**
     * 根据RoomId集合查询Room信息
     * @param roomIds 房间类型
     * @return <roomModel>list</roomModel>
     * @author 段恩刚
     * @since 2.0.0 2018-11- 27 19:34:10
     */
    @ApiOperation(value = "根据RoomId集合查询Room信息")
    @PostMapping(value = {"/queryRoomsbyIds"})
    @ApiImplicitParam(name = "roomIds", value = "roomIds", dataType = "List<String>", required = true)

    public ItooResult queryRoomsbyIds(@RequestBody List<String> roomIds) {
        List<RoomModel> roomModels = roomService.queryRoomsbyIds(roomIds);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功",roomModels);
    }
    /**
     * 下载房间信息模板，批量导入使用
     *
     * @param response 响应
     * @return 下载模板是否成功
     * @author 段恩刚
     * @since 2.0.0 2018-11-15 08:53:53
     */
  @ApiOperation(value = "下载房间信息模板，供批量导入使用", notes = "下载房间信息模板，供批量导入使用")
   @GetMapping(value = {"/downLoadTemplate"})
   public ItooResult downLoadTemplate(HttpServletResponse response) {
       if (roomService.downLoadTemplate(response)) {
           return ItooResult.build(ItooResult.SUCCESS, "下载培养计划模板成功");        } else {
           return ItooResult.build(ItooResult.FAIL, "下载培养计划模板失败");
        }
    }

    /**
     * 批量导入房间信息
     *
     * @param multipartFile 批量导入的文件
     * @param request       请求
     * @param response      响应
     * @return ItooResult 批量导入的结果
     * @author 段恩刚
     * @since 2.0.0 2018-11-27 09:05:59
     */
    @ApiOperation(value = "批量导入房间信息", notes = "0001:部分成功；0000:成功；1111:失败")
    @PostMapping(value = {"/importRoomInfoByTemplate"})
    public ItooResult importRoomInfoByTemplat(@RequestParam(value = "file") MultipartFile multipartFile,
                                              HttpServletRequest request, HttpServletResponse response) {
        return roomService.importTemplate(multipartFile, request, response);
    }

    /**
     * 导出-导入失败的数据
     *
     * @param response  响应
     * @param errorListId 为成功导入的分组id
     * @return 房间信息model
     * @author 段恩刚
     * @since 2018.11.30 19:25
     */
    @ApiOperation(value = "导出-导入失败的数据")
    @GetMapping(value = "exportFile/downErrorList/{errorListId}")
    public ItooResult downErrorList(HttpServletResponse response,
                                    @ApiParam(name = "errorListId", value = "errorListId", required = true) @PathVariable String errorListId) {

        if (roomService.downErrorList(errorListId,response)){
            return ItooResult.build(ItooResult.SUCCESS,"导出成功");
        } else {
            return ItooResult.build(ItooResult.FAIL,"导出失败");
        }

    }

    /**
     * 根据房间名称分页模糊查询房间信息
     * @param roomName  房间名称
     * @param pageNo   页码
     * @param pageSize 页数
     * @return roomModel
     * @author 段恩刚
     * @since 2018年12月15日19:44:31
     */
    @ApiOperation(value = "根据Room名称分页模糊查询Room信息", notes = "根据Room名称分页模糊查询Room信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "int", required = true, example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "记录数", dataType = "int", required = true, example = "10"),
            @ApiImplicitParam(name="roomName",value="房间名称",dataType = "string",required = true,example = "机房")
    })
    @GetMapping(value = "queryRoomsByLikeName/{roomName}/{pageNo}/{pageSize}")
    public ItooResult queryRoomsByLikeName(@RequestParam(required = false, defaultValue = "") String roomName,
                                           @PathVariable Integer pageNo, @PathVariable Integer pageSize) {
        PageInfo<RoomModel> roomModels = roomService.queryRoomsByLikeName(roomName,pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功",roomModels);
    }

    /**
     * 根据房间名称和建筑Id分页模糊查询房间信息
     * @param roomName  房间名称
     * @param buildingId
     * @param pageNo   页码
     * @param pageSize 页数
     * @return roomModel
     * @author 段恩刚
     * @since 2018年12月15日19:44:31
     */
    @ApiOperation(value = "根据Room名称和建筑Id分页模糊查询Room信息", notes = "根据Room名称和建筑Id分页模糊查询Room信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "int", required = true, example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "记录数", dataType = "int", required = true, example = "10"),
            @ApiImplicitParam(name="roomName",value="房间名称",dataType = "string",required = true,example = "机房"),
            @ApiImplicitParam(name="buildingId",value="建筑Id",dataType = "string",required = true,example = "1069881859854356481")
    })
    @GetMapping(value = "queryRoomsByLikeNameAndBuildingId/{roomName}/{buildingId}/{pageNo}/{pageSize}")
    public ItooResult queryRoomsByLikeNameAndBuildingId(@RequestParam(required = false, defaultValue = "") String roomName,
                                                        @RequestParam(required = false, defaultValue = "") String buildingId,
                                                        @PathVariable Integer pageNo, @PathVariable Integer pageSize) {
        PageInfo<RoomModel> roomModels = roomService.queryRoomsByLikeNameAndBuildingId(roomName,buildingId,pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功",roomModels);
    }
}
